Write-Host "===================================================="
Write-Host "deploy frontend start"

# 获取脚本所在的目录
$SCRIPT_DIR = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
Set-Location -Path $SCRIPT_DIR

# 获取根目录和前端目录
$ROOT_DIR = Split-Path -Path (Split-Path -Path $SCRIPT_DIR -Parent) -Parent
$PLUGIN_DIR = Join-Path $ROOT_DIR "plugins"
$FRONTEND_DIR = Join-Path $ROOT_DIR "wex-frontend"

# 确保插件目录存在
if (-Not (Test-Path -Path $PLUGIN_DIR)) {
    New-Item -ItemType Directory -Path $PLUGIN_DIR
}

# 整合扩展到项目中
Write-Host "start add plugins..."
$pluginDirs = Get-ChildItem -Directory -Path $PLUGIN_DIR
foreach ($pluginDir in $pluginDirs) {
    $frontendDir = Join-Path $pluginDir.FullName "frontend"
    if (Test-Path -Path $frontendDir) {
        $pluginName = $pluginDir.Name
        $targetDir = Join-Path $FRONTEND_DIR "plugin/plugins/$pluginName"
        if (-Not (Test-Path -Path $targetDir)) {
            New-Item -ItemType Directory -Path $targetDir
        }
        Copy-Item -Path (Join-Path $frontendDir "*") -Destination $targetDir -Recurse
        Write-Host "Moved plugin $pluginName to $targetDir"
    }
}
Write-Host "add plugins done"

# 切换到前端目录并执行命令
Set-Location -Path $FRONTEND_DIR
pnpm install
pnpm build

Write-Host "done"
Write-Host "deploy frontend done"
Write-Host "===================================================="
